<?php

namespace app\api\controller;

use app\common\exception\BusinessException;
use think\response\Json;

class Sign extends Base
{
    /**
     * 签到信息
     * @return Json
     */
    public function info(): Json
    {
        //外部参数
        $param = $this->request->param();

        //提交操作
        $response = app(\app\api\logic\Sign::class)->info($param);

        //返回
        return $this->response($response);
    }


    /**
     * 确认签到
     * @throws
     */
    public function confirm(): Json
    {
        //外部参数
        $param = $this->request->param();

        //提交操作
        app(\app\api\logic\Sign::class)->confirm($param);

        //返回
        return $this->response([], 1, '签到成功');
    }

    /**
     * 确认激励视频
     * @throws
     */
    public function rewardConfirm(): Json
    {
        //外部参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'code' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'code.require' => '用户信息异常',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        // 提交操作
        app(\app\api\logic\Sign::class)->rewardConfirm($param);

        // 返回
        return $this->response([], 1, '赠送成功');
    }

    /**
     * wxGroupWelfare
     * @return Json
     * @throws BusinessException
     */
    public function wxGroupWelfare(): Json
    {
        // 外部参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'iv' => 'require',
            'encryptedData' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'iv.require' => '安全检查iv不通过',
            'encryptedData.require' => '安全检查encryptedData不通过',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        // 提交数据
        $wxGroupId = app(\app\api\logic\Sign::class)->wxGroupWelfare($param);

        // 返回
        return $this->response(['wxGroupId' => $wxGroupId] );
    }
}
